﻿package mortal.game.view.market.buyAndQiugou
{
    import Message.Game.*;
    import com.gengine.debug.*;
    import com.mui.containers.globalVariable.*;
    import com.mui.controls.*;
    import com.mui.display.*;
    import com.mui.utils.*;
    import fl.controls.*;
    import fl.data.*;
    import flash.events.*;
    import flash.text.*;
    import mortal.common.*;
    import mortal.common.global.*;
    import mortal.component.GCatogeryList.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.info.item.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.display.*;
    import mortal.game.view.common.pageSelect.*;
    import mortal.game.view.common.util.*;
    import mortal.game.view.market.*;
    import mortal.mvc.core.*;

    public class MarketBuyPanel extends GSprite
    {
        private var _topPartContainer:GSprite;
        private var _txtSearch:TextInputList;
        private var _btnMagnifier:GLoadingButton;
        private var _comboLevel:GComboBox;
        private var _comboColor:GComboBox;
        private var _comboCareer:GComboBox;
        private var _btnSearch:GButton;
        private var _btnReset:GButton;
        private var _leftPartContainer:GSprite;
        private var _bgLeft:ScaleBitmap;
        private var _leftList:GCatogeryList;
        private var _bgMoney:ScaleBitmap;
        private var _txtMyGlod:GTextFiled;
        private var _txtMyCoin:GTextFiled;
        private var _txtNumGlod:GTextFiled;
        private var _txtNumCoin:GTextFiled;
        private var _bmpGlod:GBitmap;
        private var _bmpCoin:GBitmap;
        private var _rightPartContainer:GSprite;
        private var _bgRight:ScaleBitmap;
        private var _buyList:MktBuyList;
        private var _qiugouList:MktQiugouList;
        private var _pageSelect:PageSelecter;
        protected var _tfBai:GTextFormat;
        protected var _tfHuang:GTextFormat;
        private var _buyOrQiugou:int = 0;
        private var searchConditions:Object;

        public function MarketBuyPanel()
        {
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            this._tfBai = GlobalStyle.textFormatBai;
            this._tfBai.align = TextFormatAlign.LEFT;
            this._tfHuang = GlobalStyle.textFormatJiang;
            this._tfHuang.align = TextFormatAlign.RIGHT;
            this.addLeftPart();
            this.addTopPart();
            this.addRightPart();
            NetDispatcher.addCmdListener(ServerCommand.MarketSearchBack, this.searchBack);
            NetDispatcher.addCmdListener(ServerCommand.MarketResultBuyItem, this.buyItemBack);
            Dispatcher.addEventListener(EventName.MarketClickSortUp, this.sort);
            Dispatcher.addEventListener(EventName.MarketClickSortDown, this.sort);
            Dispatcher.addEventListener(EventName.MarketSearchClickName, this.clickItemName);
            Dispatcher.addEventListener(EventName.MarketClickType, this.clickItemNameMarketType);
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            super.disposeImpl(param1);
            this.searchConditions = null;
            NetDispatcher.removeCmdListener(ServerCommand.MarketSearchBack, this.searchBack);
            NetDispatcher.removeCmdListener(ServerCommand.MarketResultBuyItem, this.buyItemBack);
            Dispatcher.removeEventListener(EventName.MarketClickSortUp, this.sort);
            Dispatcher.removeEventListener(EventName.MarketClickSortDown, this.sort);
            Dispatcher.removeEventListener(EventName.MarketSearchClickName, this.clickItemName);
            this.removeLeftPar(param1);
            this.removeTopPar(param1);
            this.removeRightPar(param1);
            return;
        }// end function

        private function addTopPart() : void
        {
            this._topPartContainer = UIFactory.getUICompoment(GSprite, 15, 60, this);
            this._txtSearch = UIFactory.getUICompoment(TextInputList, 5, 7, this._topPartContainer);
            this._txtSearch._textInput.textField.width = this._txtSearch._textInput.textField.width - 25;
            this._txtSearch.init(this.onSearchTxtChange);
            this._btnMagnifier = UIFactory.gLoadingButton(ResFileConst.SearchBtn, this._txtSearch.x + this._txtSearch._textInput.width - 21, this._txtSearch.y + 2, 19, 19, this._topPartContainer);
            this._comboLevel = UIFactory.gComboBox(232, 7, 98, 22, null, this._topPartContainer);
            this._comboLevel.dataProvider = MktModConfig.dpLevel.clone();
            this._comboColor = UIFactory.gComboBox(338, 7, 98, 22, null, this._topPartContainer);
            this._comboColor.dataProvider = MktModConfig.dpColor.clone();
            this._comboCareer = UIFactory.gComboBox(442, 7, 98, 22, null, this._topPartContainer);
            this._comboCareer.dataProvider = MktModConfig.dpCareer.clone();
            this._btnSearch = UIFactory.gButton("搜索", 550, 7, 55, 22, this._topPartContainer);
            this._btnReset = UIFactory.gButton("重置", 605, 7, 55, 22, this._topPartContainer);
            this._btnMagnifier.configEventListener(MouseEvent.CLICK, this.clickHandler);
            this._btnSearch.configEventListener(MouseEvent.CLICK, this.clickHandler);
            this._btnReset.configEventListener(MouseEvent.CLICK, this.clickHandler);
            this.reset();
            return;
        }// end function

        private function removeTopPar(param1:Boolean) : void
        {
            this._txtSearch.dispose(param1);
            this._btnMagnifier.dispose(param1);
            this._comboLevel.dispose(param1);
            this._comboColor.dispose(param1);
            this._comboCareer.dispose(param1);
            this._btnSearch.dispose(param1);
            this._btnReset.dispose(param1);
            this._topPartContainer.dispose(param1);
            this._txtSearch = null;
            this._btnMagnifier = null;
            this._comboLevel = null;
            this._comboColor = null;
            this._comboCareer = null;
            this._btnSearch = null;
            this._btnReset = null;
            this._topPartContainer = null;
            return;
        }// end function

        private function addLeftPart() : void
        {
            this._leftPartContainer = UIFactory.getUICompoment(GSprite, 10, 92, this);
            this._bgLeft = UIFactory.bg(0, 0, 190, 420, this._leftPartContainer, ImagesConst.PanelBg);
            this._leftList = UICompomentPool.getUICompoment(GCatogeryList);
            this._leftList.setListSize(175, 355);
            this._leftList.x = 8;
            this._leftList.y = 4;
            this._leftList.tileList.direction = GBoxDirection.VERTICAL;
            this._leftList.tileList.columnWidth = 175;
            this._leftList.tileList.columnCount = 1;
            this._leftList.headGap = 0;
            this._leftPartContainer.addChild(this._leftList);
            this._leftList.scrollPane.verticalScrollPolicy = ScrollPolicy.ON;
            this._bgMoney = UIFactory.bg(0, 355, 180, 50, this._leftPartContainer, ImagesConst.TextBg);
            this._txtMyGlod = UIFactory.textField("我的钻石:", 14, 361, 70, 20, this._leftPartContainer, this._tfBai);
            this._txtMyCoin = UIFactory.textField("我的金币:", 14, 380, 70, 20, this._leftPartContainer, this._tfBai);
            this._txtNumGlod = UIFactory.textField("99999", 74, 363, 70, 20, this._leftPartContainer, this._tfHuang);
            this._txtNumCoin = UIFactory.textField("1000", 74, 382, 70, 20, this._leftPartContainer, this._tfHuang);
            this._bmpGlod = UIFactory.bitmap(ImagesConst.Yuanbao, 145, 361, this._leftPartContainer);
            this._bmpCoin = UIFactory.bitmap(ImagesConst.Jinbi, 145, 380, this._leftPartContainer);
            NetDispatcher.addCmdListener(ServerCommand.MoneyUpdate, this.updateMoney);
            this.initLeftList();
            this.updateMoney();
            return;
        }// end function

        private function removeLeftPar(param1:Boolean) : void
        {
            NetDispatcher.removeCmdListener(ServerCommand.MoneyUpdate, this.updateMoney);
            this._bgLeft.dispose(param1);
            this._leftList.dispose(param1);
            this._bgMoney.dispose(param1);
            this._txtMyGlod.dispose(param1);
            this._txtMyCoin.dispose(param1);
            this._txtNumGlod.dispose(param1);
            this._txtNumCoin.dispose(param1);
            this._bmpGlod.dispose(param1);
            this._bmpCoin.dispose(param1);
            this._leftPartContainer.dispose(param1);
            this._bgLeft = null;
            this._leftList = null;
            this._bgMoney = null;
            this._txtMyGlod = null;
            this._txtMyCoin = null;
            this._txtNumGlod = null;
            this._txtNumCoin = null;
            this._bmpGlod = null;
            this._bmpCoin = null;
            this._leftPartContainer = null;
            return;
        }// end function

        private function addRightPart() : void
        {
            this._rightPartContainer = UIFactory.getUICompoment(GSprite, 200, 92, this);
            this._bgRight = UIFactory.bg(0, 0, 484, 420, this._rightPartContainer, ImagesConst.PanelBg);
            this._pageSelect = UIFactory.pageSelecter(174, 385, this._rightPartContainer, PageSelecter.CompleteMode);
            this._pageSelect.setbgStlye(ImagesConst.ComboBg, new GTextFormat());
            this._pageSelect.maxPage = 1;
            this._pageSelect.pageTextBoxSize = 36;
            this._pageSelect.configEventListener(Event.CHANGE, this.onPageChange);
            return;
        }// end function

        private function removeRightPar(param1:Boolean) : void
        {
            this._bgRight.dispose(param1);
            this._pageSelect.dispose(param1);
            if (this._qiugouList)
            {
                this._qiugouList.dispose(param1);
                this._qiugouList.visible = true;
                this._qiugouList = null;
            }
            if (this._buyList)
            {
                this._buyList.dispose(param1);
                this._buyList.visible = true;
                this._buyList = null;
            }
            this._rightPartContainer.dispose(param1);
            this._bgRight = null;
            this._pageSelect = null;
            this._rightPartContainer = null;
            return;
        }// end function

        public function initLeftList() : void
        {
            var _loc_1:* = MktModConfig.arrItemType;
            this._leftList.createHeads(MktCatogeryHead, _loc_1, 160, 23);
            this._leftList.setCellRender(0, MktCatogeryRenderer, true);
            this._leftList.setCellHeight(0, 23, true);
            this._leftList.setListItemHeight(23);
            var _loc_2:int = 0;
            while (_loc_2 < MktModConfig.arrItemType.length)
            {
                
                this._leftList.setDataProvider(_loc_2, MktModConfig.getdpItemType2((_loc_2 + 1)));
                _loc_2++;
            }
            return;
        }// end function

        private function onSearchTxtChange() : void
        {
            if (this._txtSearch._textInput.text.length >= 2)
            {
                this._txtSearch.updateDataProvider(this.getTxtListDp());
            }
            return;
        }// end function

        private function clickHandler(event:MouseEvent) : void
        {
            if (event.currentTarget == this._btnMagnifier)
            {
                this.searchByName();
            }
            else if (event.currentTarget == this._btnSearch)
            {
                this.searchByConditions();
            }
            else if (event.currentTarget == this._btnReset)
            {
                this.reset();
            }
            return;
        }// end function

        private function reset() : void
        {
            this._comboLevel.selectedIndex = 0;
            this._comboColor.selectedIndex = 0;
            this._comboCareer.selectedIndex = 0;
            this._leftList.unexpandAllItem();
            return;
        }// end function

        public function resetAll() : void
        {
            this.reset();
            this._pageSelect.currentPage = 1;
            return;
        }// end function

        private function onPageChange(event:Event) : void
        {
            this.searchConditions.targetPage = this._pageSelect.currentPage;
            this.search();
            return;
        }// end function

        private function sort(event:DataEvent) : void
        {
            if (event.type == EventName.MarketClickSortUp)
            {
                if (this.buyOrQiugou == 1)
                {
                    this.searchConditions.order = EMarketOrder._EMarketOrderUnitPriceAsc;
                }
                else
                {
                    this.searchConditions.order = EMarketOrder._EMarketOrderAsc;
                }
            }
            else if (event.type == EventName.MarketClickSortDown)
            {
                if (this.buyOrQiugou == 1)
                {
                    this.searchConditions.order = EMarketOrder._EMarketOrderUnitPriceDesc;
                }
                else
                {
                    this.searchConditions.order = EMarketOrder._EMarketOrderDesc;
                }
            }
            else
            {
                return;
            }
            this.search();
            return;
        }// end function

        private function clickItemName(event:DataEvent) : void
        {
            var _loc_2:* = event.data as SMarketItem;
            if (_loc_2 != null)
            {
                this.searchByName([_loc_2.code]);
            }
            return;
        }// end function

        private function clickItemNameMarketType(event:DataEvent) : void
        {
            this.searchByItemType(event.data.tMarket.marketId);
            return;
        }// end function

        private function searchByName(param1:Array = null) : void
        {
            var _loc_2:Object = {};
            _loc_2.recordType = this.marketRecordType;
            _loc_2.marketId = -1;
            if (param1 == null)
            {
                if (this._txtSearch.selectdata == null)
                {
                    MsgManager.showRollTipsMsg("请输入正确的道具名字");
                    return;
                }
                _loc_2.codes = [(this._txtSearch.selectdata as ItemInfo).code];
            }
            else
            {
                _loc_2.codes = param1;
            }
            _loc_2.targetPage = 1;
            _loc_2.levelLower = this._comboLevel.getItemAt(0).min;
            _loc_2.levelUpper = this._comboLevel.getItemAt(0).max;
            _loc_2.color = this._comboColor.getItemAt(0).color;
            _loc_2.career = this._comboCareer.getItemAt(0).career;
            _loc_2.order = EMarketOrder._EMarketOrderNormal;
            this.searchConditions = _loc_2;
            this.search();
            return;
        }// end function

        private function searchByItemType(param1:int = -1) : void
        {
            var _loc_2:Object = {};
            _loc_2.recordType = this.marketRecordType;
            if (param1 != -1)
            {
                _loc_2.marketId = param1;
            }
            else if (this._leftList.tileList.dataProvider.length > 0 && this._leftList.tileList.selectedItem)
            {
                _loc_2.marketId = this._leftList.tileList.selectedItem.tMarket.marketId;
            }
            else
            {
                _loc_2.marketId = -1;
            }
            _loc_2.codes = new Array();
            _loc_2.targetPage = 1;
            _loc_2.levelLower = this._comboLevel.getItemAt(0).min;
            _loc_2.levelUpper = this._comboLevel.getItemAt(0).max;
            _loc_2.color = this._comboColor.getItemAt(0).color;
            _loc_2.career = this._comboCareer.getItemAt(0).career;
            _loc_2.order = EMarketOrder._EMarketOrderNormal;
            this.searchConditions = _loc_2;
            this.search();
            return;
        }// end function

        private function searchByConditions() : void
        {
            var _loc_1:Object = {};
            _loc_1.recordType = this.marketRecordType;
            _loc_1.marketId = -1;
            _loc_1.codes = new Array();
            _loc_1.targetPage = 1;
            _loc_1.levelLower = this._comboLevel.selectedItem.min;
            _loc_1.levelUpper = this._comboLevel.selectedItem.max;
            _loc_1.color = this._comboColor.selectedItem.color;
            _loc_1.career = this._comboCareer.selectedItem.career;
            _loc_1.order = EMarketOrder._EMarketOrderNormal;
            _loc_1.playerName = "";
            this.searchConditions = _loc_1;
            this.search();
            return;
        }// end function

        public function search() : void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.MarketSearch, this.searchConditions));
            return;
        }// end function

        private function buyItemBack(param1:Object) : void
        {
            this.search();
            return;
        }// end function

        private function searchBack(param1:Object) : void
        {
            this.updateList();
            return;
        }// end function

        private function updateList() : void
        {
            if (Cache.instance.market.marketItemObj.marketItems.length == 0)
            {
            }
            if (this.buyOrQiugou == 1 && Cache.instance.market.marketItemObj.recordType == EMarketRecordType._EMarketRecordSell)
            {
                this._buyList.update();
            }
            else if (this.buyOrQiugou == 2 && Cache.instance.market.marketItemObj.recordType == EMarketRecordType._EMarketRecordSeekBuy)
            {
                this._qiugouList.update();
            }
            this._pageSelect.currentPage = Cache.instance.market.marketItemObj.targetPage;
            this._pageSelect.maxPage = Cache.instance.market.marketItemObj.totalPage;
            return;
        }// end function

        private function updateMoney(param1:Object = null) : void
        {
            var _loc_2:* = Cache.instance.role.money;
            this._txtNumCoin.htmlText = MoneyUtil.getCoinHtml(_loc_2.coin);
            this._txtNumGlod.htmlText = MoneyUtil.getCoinHtml(_loc_2.gold);
            return;
        }// end function

        private function get marketRecordType() : int
        {
            if (this.buyOrQiugou == 1)
            {
                return EMarketRecordType._EMarketRecordSell;
            }
            if (this.buyOrQiugou == 2)
            {
                return EMarketRecordType._EMarketRecordSeekBuy;
            }
            return 0;
        }// end function

        public function get buyOrQiugou() : int
        {
            return this._buyOrQiugou;
        }// end function

        public function set buyOrQiugou(param1:int) : void
        {
            this._buyOrQiugou = param1;
            this.resetAll();
            if (this.buyOrQiugou == 1)
            {
                if (this._qiugouList)
                {
                    this._qiugouList.visible = false;
                }
                if (this._buyList == null)
                {
                    this._buyList = UIFactory.getUICompoment(MktBuyList, 0, 0, this._rightPartContainer);
                }
                this._buyList.visible = true;
                this._rightPartContainer.addChild(this._buyList);
                Log.debug("-----_buyList", this._buyList.mouseEnabled, this._buyList.mouseChildren);
            }
            else if (this.buyOrQiugou == 2)
            {
                if (this._buyList)
                {
                    this._buyList.visible = false;
                }
                if (this._qiugouList == null)
                {
                    this._qiugouList = UIFactory.getUICompoment(MktQiugouList, 0, 0, this._rightPartContainer);
                }
                this._qiugouList.visible = true;
                this._rightPartContainer.addChild(this._qiugouList);
                Log.debug("-----_qiugouList", this._qiugouList.mouseEnabled, this._qiugouList.mouseChildren);
            }
            this.searchByConditions();
            return;
        }// end function

        private function getTxtListDp() : DataProvider
        {
            var _loc_1:* = MarketCache.searchByName(this._txtSearch._textInput.text);
            return new DataProvider(_loc_1);
        }// end function

    }
}
